import request from '@/utils/request'
// import axios from 'axios'

// 注册用户名
export const RegisterUser = ({ username, password, repassword }) => {
  return request({
    url: '/api/reg',
    method: 'post',
    data: {
      username,
      password,
      repassword
    }
  })
}

// 登录功能
export const UserLogin = ({ username, password }) => {
  return request({
    url: '/api/login',
    method: 'post',
    data: {
      username,
      password
    }
  })
}

// 获取登录用户信息资料
export const GetUser = () => {
  return request.get('/my/userinfo')
}

// 获取文章列表
export const GetArticle = ({ pagenum, pagesize, state, cateid }) => {
  return request({
    url: '/my/article/list',
    params: {
      pagenum,
      pagesize,
      state,
      cate_id: cateid
    }
  })
}

// 获取文章分类
export const GetArticleAssort = () => {
  return request.get('/my/cate/list')
}

// 新增文章分类
export const AddArticleAssort = (obj) => {
  // 接口数据格式为 JSON  转为JSON后提交
  return request({
    url: '/my/cate/add',
    method: 'post',
    data: {
      cate_name: obj.cate_name,
      cate_alias: obj.cate_alias
    }
  })
}

// 删除文章分类
export const DelArticleAssort = (id) => {
  return request({
    url: '/my/cate/del',
    method: 'delete',
    params: {
      id
    }
  })
}

// 修改文章分类
export const PutArticleAssort = (obj) => {
  return request({
    url: '/my/cate/info',
    method: 'put',
    data: {
      id: obj.id,
      cate_name: obj.cate_name,
      cate_alias: obj.cate_alias
    }
  })
}

// 发布文章
export const ReleaseArticleAssort = (obj) => {
  // console.log(obj)
  const data = new FormData()
  data.append('title', obj.title)
  data.append('cate_id', obj.cate_id)
  data.append('content', obj.content)
  data.append('state', obj.state)
  data.append('cover_img', obj.cover_img)

  return request({
    url: '/my/article/add',
    method: 'POST',
    data,
    headers: {
      Authorization: localStorage.getItem('token')
    }
  })
}

// 获取单个文章
export const GetArticleId = (id) => {
  return request({
    url: '/my/article/info',
    method: 'get',
    params: {
      id
    }
  })
}
// 修改文章内容
export const PutArticleId = (obj) => {
  console.log(obj)
  const data = new FormData()
  data.append('id', obj.id)
  data.append('title', obj.title)
  data.append('cate_id', obj.cate_id)
  data.append('content', obj.content)
  data.append('state', obj.state)
  data.append('cover_img', obj.cover_img)

  return request({
    url: '/my/article/info',
    method: 'put',
    data,
    headers: {
      Authorization: localStorage.getItem('token')
    }
  })
}
// 删除文章
export const DelArticle = (id) => {
  return request({
    url: '/my/article/info',
    method: 'delete',
    params: {
      id
    }
  })
}

// 修改用户基本资料
export const PutUserInfo = (UserInfo) => {
  return request({
    url: '/my/userinfo',
    method: 'PUT',
    data: UserInfo
  })
}

// 修改密码
export const UpdatePassword = (UserInfo) => {
  return request({
    url: '/my/updatepwd',
    method: 'PATCH',
    data: UserInfo
  })
}

// 上传(更换)头像
export const UpdateAvatar = (avatar) => {
  return request({
    url: '/my/update/avatar',
    method: 'PATCH',
    data: {
      avatar: avatar
    }
  })
}
